local pokeen = class("Pokeengine")
local T = require "packages/jbs/record/Theorem"

--TODO 速度能力实装

--选择目标
pokeen.choicetarget=function (room,player,str)
    if str~=0 then
    local targets=table.map(table.filter(room.alive_players,function (p)
        return p~=player
      end),function(p) return p.id end)
    if #targets==1 then
    local tar=room:getPlayerById(targets[1])
    return tar
    end
    else
    return player
    end
end



--生成伤害
pokeen.enddamagecount=function (att,def,power)
    
  local s=power or 50 --威力
  local random=math.random(217,255) --随机系数
  local att=att or 400
  local def=def or 200
  local s=42*s*att
  local y=def
  local sy=(s/y/50)+2
  local damage=(sy*1.5*random)/255
    return damage//1
end

--修正倍数
pokeen.damagelv=function (n)
    if n>=0 then
        return (n+2)/2
    else 
        return  2/(2-n)
    end

end
--设置技能属性威力，命中率，暴击率，类别1为攻击，是否自身，先发度
pokeen.setproperty=function (jufeng,str)
    local str=str or {100,1,0.2,1,1,0}
    --威力
    jufeng.pow=str[1]
--命中率
jufeng.hitrate=str[2]
--暴击率
jufeng.critrate=str[3]
--技能类型1为攻击
jufeng.type=str[4]
--是否为自己
jufeng.istoself=str[5]
jufeng.first=str[6]
    
end

--实际造成伤害（受能力等级影响）
pokeen.causedamage=function (p,t,skill)
    local power=skill.power or 60
    local atklv=pokeen.getPowerlv(p,"atk")
    local deflv=pokeen.getPowerlv(t,"def")
    local atkbuff=pokeen.damagelv(atklv)
    local defbuff=pokeen.damagelv(deflv)
    local atk=p.atk*atkbuff or 300
    local def=t.def*defbuff or 200
    local damage=pokeen.enddamagecount(atk,def,power)
    
    local critrate=skill.critrate or 0
    local critlv=pokeen.getPowerlv(p,"crit")
    if pokeen.random(critrate+critlv*0.1) then
         damage=damage*2 
         p. room:sendLog {
            type = "#pokemon_crit_log",
            
          }
    end
    p.room:damage{
        from = p,
        to = t,
        damage = damage,
        skillName=skill.name
    }

end

pokeen.misslv=function (lv)
    return lv*0.8

end
--命中判定
pokeen.missjudge=function (skill,p,t)
    local hitrate=skill.hitrate or 1
    if hitrate>1 then
        return true
    else
        local hitlv=pokeen.getPowerlv(p,"shot")
        local misslv=pokeen.getPowerlv(t,"miss")
        local truelv=hitlv-misslv --时机等级：自身命中等级-对手闪避等级
        local rate=pokeen.misslv(truelv)
        return pokeen.random(hitrate*(1+rate))

    end
end
--发战报
pokeen.sendlong=function (room,str)
room:sendLog {
    type = str,
   
  }
end

---@param  generaldata table@武将数据的表，由拓展，名称，势力，血量，上限，性别组成
---@param totrans table@由武将名，称号，作者，画师构成的表,有默认值
---@param pokemonstat table@宝可梦数值的表，表中数据依次为攻击，防御，速度，技能组
pokeen.creaternewpokemon=function (generaldata,totrans,pokemonstat)
    local poke=T.creategenandtanslatene(generaldata,totrans)
    poke.atk=pokemonstat[1] or 300
    poke.def=pokemonstat[2] or 200
    poke.spd=pokemonstat[3] or 300
    poke.skill_pool=pokemonstat[4] or {"poke__siguang","jbspoke_penshe","poke__siguang","poke__siguang"}
    return poke
end

Fk:addQmlMark{
    name = "shuxing",
    how_to_show = function(name, value)

        return " "
    end,
    qml_path = "packages/jbs2/qml/shuxing"
  }

T.translate("shuxing","面板")

--获取玩家能力等级，str可选值为"atk","def","spe","shot","miss"
pokeen.getPowerlv=function (player,str)
    local mark={"atk","def","spe","shot","miss","crit"}
    if not table.contains(mark,str) then return end
    local pdata=player:getMark("@[shuxing]")
    local data=pdata.propertys
    local i=table.indexOf(mark, str)
    return data[i+3]
end

--设定玩家某项能力等级，且最少为-6，最高为+6
pokeen.setPowerlv=function (player,str,n)
    local mark={"atk","def","spe","shot","miss","crit"}
    if not table.contains(mark,str) then return end
    local room=player.room
    local pdata=player:getMark("@[shuxing]")
    local data=pdata.propertys
    local i=table.indexOf(mark, str)
    local n=math.max(math.min(n,6),-6)
    data[i+3]=n
    pdata.propertys=data
    room:setPlayerMark(player, "@[shuxing]",pdata)

end

--改变玩家某项能力等级，且最少为-6，最高为+6
pokeen.changePowerlv=function (player,str,n)
    local mark={"atk","def","spe","shot","miss","crit"}
    local room=player.room
    if not table.contains(mark,str) then return end
    local currentlv=pokeen.getPowerlv (player,str)
    local tochange=math.max(math.min(currentlv+n,6),-6)
    pokeen.setPowerlv(player,str,tochange)
    room:sendLog {
        type = "#pokemon_changePower_log",
        from = player.id,
        arg =str,
        arg2 =n,
        
      }


end




--改变玩家若干项能力等级,默认为全属性+1
pokeen.changePowerlvs=function (player,strs,nums)
    local mark={"atk","def","spe","shot","miss","crit"}
    local strs=strs or mark
    for i,str in ipairs(strs) do
    if not table.contains(mark,str) then return end
    local n=(nums[i] or nums[1] )or 1
    pokeen.changePowerlv(player,str,n)
    
    end

end

--获取玩家所有能力等级

--出手先后比较
pokeen.whofirest=function (p,t)
    local pp={}
    local first1=p.choice.first or 0
    local first2=t.choice.first or 0
    local spdlv1=pokeen.getPowerlv(p,"spe")
    local spdlv2=pokeen.getPowerlv(t,"spe")
    if first1 > first2 then
        table.insert(pp,p)
        table.insert(pp,t)

    elseif first1 < first2 then
        table.insert(pp,t)
        table.insert(pp,p)
    else
        -- 技能先发度相同，比较速度
        if p.spd*(1+pokeen.damagelv(spdlv1)) > t.spd*(1+pokeen.damagelv(spdlv2)) then
            table.insert(pp,p)
            table.insert(pp,t)
        elseif p.spd*(1+pokeen.damagelv(spdlv1)) < t.spd*(1+pokeen.damagelv(spdlv2)) then
            table.insert(pp,t)
            table.insert(pp,p)
        else
            pp={p,t}
            table.shuffle(pp)
        end
    end
    return pp
    
end

---技能概率
pokeen.random=function (n)
    local x=math.random()
    return x<n
    
end
return pokeen